package com.jus.player.adapter.home;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.jus.player.R;
import com.jus.player.activity.home.JsPlayerActivity;
import com.jus.player.config.AppConfig;
import com.jus.player.entity.VideoEntity;
import com.jus.player.util.StringUtils;

import java.util.List;

public class SearchHotAdapter extends RecyclerView.Adapter<SearchHotAdapter.ViewHolder> {
    private List<VideoEntity> videoList;
    public void setVideoList(List<VideoEntity> videoList) {
        this.videoList = videoList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_search_hot, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        VideoEntity video = videoList.get(position);
        // 假设这里使用Glide加载视频缩略图
        if (!StringUtils.isEmpty(video.getTvCoverUrl())) {
            Glide.with(holder.itemView.getContext())
                    .load(video.getTvCoverUrl())
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(32)))//圆角
                    .into(holder.thumbImg);
        }
        holder.title.setText(video.getTitle());
        holder.subTitle.setText(video.getSubTitle());
        //holder.updateEpisode.setText("更新至第 " + video.getUpdateEpisode() + " 集");
        holder.tvId = video.getId();
        holder.lastEpisode = video.getLastEpisode() == null ? AppConfig.INT_1 : video.getLastEpisode();

    }

    @Override
    public int getItemCount() {
        return videoList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public RelativeLayout searchContainer;
        public ImageView thumbImg;
        public TextView title;
        public TextView subTitle;
        //public TextView updateEpisode;

        public Long tvId;
        public Integer lastEpisode;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            thumbImg = itemView.findViewById(R.id.video_thumb);
            searchContainer = itemView.findViewById(R.id.item_search_container);
            title = itemView.findViewById(R.id.title);
            subTitle = itemView.findViewById(R.id.sub_title);
            //updateEpisode = itemView.findViewById(R.id.updateEpisode);
            //视频列表点击事件
            searchContainer.setOnClickListener(v ->
                    JsPlayerActivity.start(itemView.getContext(), lastEpisode - 1, tvId, false));
        }
    }
}
